package org.example

object s1 {
  def main(args: Array[String]): Unit = {
    def addNum: Int => Int = (X: Int) => X * 2

    val list1 = List(100, 200, 300).map(addNum)
    val list2 = List(100, 200, 300).map(x => x * 2)
    val list3 = List(100, 200, 300).map(_*2)
    println(list3)

 //练习：
    val s ="nba"
    println(s.reverse)
    val wordList = List("java", "scala", "cba")
    println(wordList.map(_.reverse))
 //逆序输出
    println(wordList.reverse)
    val score = Array(("张三", Array(5.5, 6.6, 7.7, 8.8, 8.0)), ("李四", Array(6.0, 6.5, 7.0, 7.5, 9.0)))
    score.map(x => (x._1, x._2.max)).foreach(println)
    score.map(tp => (tp._1, tp._2.map(t => if (t >= 8.0) 1 else 0).sum)).foreach(println)

 //求张三和李四低于6.0分的昌吉门数和平均分
    score.map(tp => (tp._1,tp._2.map(t => if(t <6.0) 1 else 0).sum)).foreach(println)
    score.map(tp => (tp._1,tp._2.count(t => t < 6.0))).foreach(println)
    score.map(sp => (sp._1,sp._2.sum / sp._2.length)).foreach(println)
 //2.filter过滤 类型和元素不变
    val arr1 =Array(1,2,3,4,5,6)
    arr1.filter(t => if (t % 2 == 0)true else false).foreach(println)
    arr1.filter(t => t % 2 == 0).foreach(println)
    arr1.filter(_ % 2 == 1).foreach(println)


 //输出集合中的回文单词
    val arr2 = Array("I","love","dad")
    arr2.filter(word => word.reverse == word).foreach(println)

// 3.flatMap扁平映射
    val wordFlat = List("I am hero"," you are smart","I love scala")
// 分词
    val wordSplit = wordFlat.map(_.split(""))
// 打散扁平化
    val wordFlatten = wordSplit.flatten
    println(wordFlatten)

    val flatmapList = wordFlat.flatMap(_.split(""))
    println(flatmapList)

// 4.groupBy 分组
     println(flatmapList.groupBy(_.charAt(0)).toList)


// 练习：wordFlat中相同单词分组，输出每个单词出现的次数
    //1、分组
    val wordSam = flatmapList.groupBy(word => word)
    println(wordSam)
    //2、统计次数
    val countMap = wordSam.map(tp => (tp._1,tp._2.length))
    println(countMap)
    //3、哈希表转为列表
    val countList = countMap.toList
    print(countList)
    //4、排序
    val sortList = countList.sortWith(_._2 > _._2)
    println(sortList)
    //5、取前2
    val takeList = countList.take(2)
    println(takeList)


//读取word.txt文档，统计出词频最高的前三个单词并输出 找出回文单词（教材48实训1）并输出


//  文档读取

//    val words = Source.fromFile("src/main/resources/word.txt")
//    words.foreach(print)

  }

}
